#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using CustomsNuke.Engine.Data;

#endregion

namespace CustomsNuke.BusinessSystem.Categories.CustomsAdmission
{
    public partial class CreateCommonTaskForm : Form
    {
        private CustomsNukeContext customsNukeContext = new CustomsNukeContext();
        private List<RunningFrequencyData> runningFrequencies;
        private List<CheckBox> businessTypeCheckBoxes = new List<CheckBox>();

        public int BusinessId
        {
            set
            {
                var cb = this.businessTypeCheckBoxes.First(p => Int32.Parse(p.Tag.ToString()) == value);
                if (cb != null)
                {
                    cb.Checked = true;
                }
            }
        }

        public ModelWindowStatus Status { get; set; }

        public CreateCommonTaskForm()
        {
            this.runningFrequencies = new List<RunningFrequencyData>() { 
                new RunningFrequencyData() { Value = 60, DisplayText = "1小时" },
                new RunningFrequencyData() { Value = 120, DisplayText = "2小时" },
                new RunningFrequencyData() { Value = 180, DisplayText = "3小时" },
                new RunningFrequencyData() { Value = 240, DisplayText = "4小时" },
                new RunningFrequencyData() { Value = 480, DisplayText = "8小时" },
                new RunningFrequencyData() { Value = 1440, DisplayText = "1天" }
            };

            InitializeComponent();

            this.RunningFrequency.Items.AddRange(this.runningFrequencies);

            this.businessTypeCheckBoxes.Add(this.BusinessType1);
            this.businessTypeCheckBoxes.Add(this.BusinessType2);
        }

        private void CreateCustomsAdmissionTaskForm_Load(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            this.Save();
            DialogResult = DialogResult.OK;
            this.Close();
        }
        private void Save()
        {
            var containerNumbers = this.ContainerNumber.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var selectedBusiness =
                from cb in this.businessTypeCheckBoxes
                where cb.Checked == true
                select cb.Tag;

            if (containerNumbers != null)
            {
                foreach (var containerNumber in containerNumbers)
                {
                    foreach (var business in selectedBusiness.ToList())
                    {
                        var businessId = 0;
                        Int32.TryParse(business.ToString(), out businessId);

                        var task = this.CreateTask();
                        task.BusinessId = businessId;
                        task.ParameterContainerNumber = containerNumber;
                        task.RunningStartTime = this.RunningStartTime.Value;
                        task.RunningDuration = (int)this.RunningEndTime.Value.AddDays(1).Subtract(this.RunningStartTime.Value).TotalMinutes;
                        task.RunningFrequency = ((RunningFrequencyData)this.RunningFrequency.SelectedItem).Value;

                        this.customsNukeContext.AddToTask(task);
                    }
                }
            }

            this.customsNukeContext.SaveChanges();
        }

        private Task CreateTask()
        {
            Task task = new Task()
            {
                IsDeleted = false,
                CreatedDate = DateTime.Now,
                ModifiedDate = DateTime.Now,
                UserId = 1,
                TaskStateId = 1
            };

            return task;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }

    public class RunningFrequencyData
    {
        public int Value { get; set; }
        public string DisplayText { get; set; }
        public override string ToString()
        {
            return this.DisplayText;
        }
    }
}